﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace QuanLyBepAnTuThien
{
    public partial class frmUngHoTien : Form
    {
        public frmUngHoTien()
        {
            InitializeComponent();
        }
        connect cn = new connect();
        private void loaddata()
        {
            cobMTQ.DataSource = cn.getDataTable("tblMTQ_DS");
            cobMTQ.DisplayMember = "HoTen";
            cobMTQ.ValueMember = "MaMTQ";            

            cobNguoiNhan.DataSource = cn.getDataTable("tblNhanVien_DS");
            cobNguoiNhan.DisplayMember = "HoTen";
            cobNguoiNhan.ValueMember = "MaNV";
        }
        private void frmUngHoTien_Load(object sender, EventArgs e)
        {
            loaddata();
            if (bientinhSoVang.Thaotac == true)
            {
                cobMTQ.SelectedValue = bientinhSoVang.Mamtq;
                cobNguoiNhan.Text = bientinhSoVang.Hotennv;
                txtSoTien.Text = bientinhSoVang.Sotien.ToString();
                btnThem.SendToBack();
            }
            else
            {
                btnThem.BringToFront();
            }
        }

        private void txtSoTien_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }
        private bool kiemtrahotenmtq()
        {
            SqlCommand cmd = new SqlCommand("tblMTQ_KiemTraHoTen", cn.ketnoi());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("ten", SqlDbType.NVarChar)).Value = cobMTQ.Text;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            if (dt.Rows.Count <= 0)
            {
                return false;
            }
            return true;
        }
        private bool kiemtrahotennv()
        {
            SqlCommand cmd = new SqlCommand("tblNhanVien_KiemTraTenNV", cn.ketnoi());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("tennv", SqlDbType.NVarChar)).Value = cobNguoiNhan.Text;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            if (dt.Rows.Count <= 0)
            {
                return false;
            }
            return true;
        }
        private void them()
        {
            if (kiemtrahotenmtq() == false)
            {
                MessageBox.Show("Nhân viên " + cobMTQ.Text + " chưa có trong danh sách nhân viên", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                cobMTQ.Focus();
            }
            else if (kiemtrahotennv() == false)
            {
                MessageBox.Show("Nhân viên " + cobNguoiNhan.Text + " chưa có trong danh sách nhân viên", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                cobNguoiNhan.Focus();
            }
            else if (txtSoTien.Text == "")
            {
                MessageBox.Show("Bạn chưa nhập số tiền ủng hộ", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtSoTien.Focus();
            }

            else
            {
                SqlCommand cmd = new SqlCommand("tblSoVang_Them", cn.ketnoi());
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("mamtq", SqlDbType.Int)).Value = cobMTQ.SelectedValue;
                cmd.Parameters.Add(new SqlParameter("ngay", SqlDbType.DateTime)).Value = dtNgayUngHo.Value;
                cmd.Parameters.Add(new SqlParameter("matv", SqlDbType.VarChar)).Value = cobNguoiNhan.SelectedValue;
                cmd.Parameters.Add(new SqlParameter("sotien", SqlDbType.Float)).Value = double.Parse(txtSoTien.Text);
                if (cmd.ExecuteNonQuery() == 1)
                {
                    MessageBox.Show("Thêm Ủng hộ thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                }
            }                
        }
        private void frmUngHoTien_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                them();
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            them();
        }

        private void txtSoTien_TextChanged(object sender, EventArgs e)
        {
            txtSoTien.Text = String.Format("{0:N0}", double.Parse(txtSoTien.Text));
            txtSoTien.SelectionStart = txtSoTien.Text.Length;              
        }

        private void btnThemMTQ_Click(object sender, EventArgs e)
        {
            frmThemMTQ _frmThemMTQ = new frmThemMTQ();
            _frmThemMTQ.ShowDialog();
            loaddata();
        }

        private void tnSua_Click(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand("tblSoVang_SuaUngHoTien",cn.ketnoi());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("stt",SqlDbType.Int)).Value = bientinhSoVang.Sttsovang;
            cmd.Parameters.Add(new SqlParameter("mamtq", SqlDbType.Int)).Value = cobMTQ.SelectedValue;
            cmd.Parameters.Add(new SqlParameter("ngayungho", SqlDbType.DateTime)).Value = dtNgayUngHo.Value;
            cmd.Parameters.Add(new SqlParameter("matv", SqlDbType.VarChar)).Value = cobNguoiNhan.SelectedValue;
            cmd.Parameters.Add(new SqlParameter("sotien", SqlDbType.Float)).Value = txtSoTien.Text;
            if (cmd.ExecuteNonQuery() == 1)
            {
                MessageBox.Show("Sửa thông tin thành công","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Information);
                this.Close();
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            this.Close();
        }
       
    }
}

